package com.gitee.urick.union.utils;

import com.power.common.util.StringUtil;
import com.power.doc.constants.DocGlobalConstants;
import com.power.doc.utils.DocUtil;
import com.thoughtworks.qdox.model.DocletTag;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaMember;
import com.thoughtworks.qdox.model.JavaMethod;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Rick 2021/02/08
 * @since 1.0
 */
public class UnionDocUtil extends DocUtil {
    /**
     * obtain params comments
     *
     * @param javaClass JavaMethod
     * @param tagName   java comments tag
     * @param className class name
     * @return Map
     */
    public static Map<String, String> getParamsComments(final JavaClass javaClass, final String tagName, final String className) {
        List<DocletTag> paramTags = javaClass.getTagsByName(tagName);
        Map<String, String> paramTagMap = new HashMap<>();
        for (DocletTag docletTag : paramTags) {
            String value = docletTag.getValue();
            if (StringUtil.isEmpty(value) && StringUtil.isNotEmpty(className)) {
                throw new RuntimeException("ERROR: #" + javaClass.getName()
                        + "() - bad @" + tagName + " javadoc from " + javaClass.getCanonicalName() + ", must be add comment if you use it.");
            }
            String pName;
            String pValue;
            int idx = value.indexOf("\n");
            //existed \n
            if (idx > -1) {
                pName = value.substring(0, idx);
                pValue = value.substring(idx + 1);
            } else {
                pName = (value.contains(" ")) ? value.substring(0, value.indexOf(" ")) : value;
                pValue = value.contains(" ") ? value.substring(value.indexOf(' ') + 1) : DocGlobalConstants.NO_COMMENTS_FOUND;
            }
            paramTagMap.put(pName, pValue);
        }
        return paramTagMap;
    }

    /**
     * obtain java doc tags comments,like apiNote
     *
     * @param javaClass JavaClass
     * @param tagName   java comments tag
     * @param className class name
     * @return Map
     */
    public static String getNormalTagComments(final JavaClass javaClass, final String tagName, final String className) {
        Map<String, String> map = getParamsComments(javaClass, tagName, className);
        return getFirstKeyAndValue(map);
    }

    /**
     * builder http header by comments
     *
     * @param javaClass JavaClass
     * @param tagName   java comments tag
     * @return headers
     */
    public static String getHeaderComments(final JavaClass javaClass, final String tagName) {
        List<DocletTag> tags = javaClass.getTagsByName(tagName);
        return buildHeader(tags);
    }
    /**
     * builder http header by comments
     *
     * @param javaMethod JavaMethod
     * @param tagName   java comments tag
     * @return headers
     */
    public static String getHeaderComments(final JavaMethod javaMethod, final String tagName) {
        List<DocletTag> tags = javaMethod.getTagsByName(tagName);
        return buildHeader(tags);
    }

    /**
     * builder http header
     */
    private static String buildHeader(List<DocletTag> tags) {
        StringBuilder headers = new StringBuilder();
        for (DocletTag docletTag : tags) {
            String value = docletTag.getValue();
            if (StringUtil.isEmpty(value)) continue;
            headers.append(value);
            headers.append("\n");
        }
        return headers.toString();
    }
}
